//
//  CFBTTabBarVC.m
//  半糖框架
//
//  Created by afro on 15/12/9.
//  Copyright © 2015年 afro. All rights reserved.
//

#import "CFBTTabBarVC.h"
#import "BTTabBar.h"
#import "CFBTHomePageVC.h"
#import "CFBTHomePageNavVC.h"

@interface CFBTTabBarVC () <BTTabBarDelegate>

@end

@implementation CFBTTabBarVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    CFBTHomePageVC *homePageVC = [[CFBTHomePageVC alloc]init];
    [self addChildVc:homePageVC title:@"半糖" image:@"tab_首页" selectedImage:@"tab_首页_pressed"];
    
    UIViewController *vc2 = [[UIViewController alloc] init];
    [self addChildVc:vc2 title:@"精选|关注" image:@"tab_社区" selectedImage:@"tab_社区_pressed"];
    
    UIViewController *vc3 = [[UIViewController alloc] init];
    [self addChildVc:vc3 title:@"分类" image:@"tab_分类" selectedImage:@"tab_分类_pressed"];
    
    UIViewController *vc4 = [[UIViewController alloc] init];
    [self addChildVc:vc4 title:@"好物" image:@"tab_我的" selectedImage:@"tab_我的_pressed"];
    
    // 2.更换系统自带的tabbar
    BTTabBar *tabBar = [[BTTabBar alloc] init];
    tabBar.delegate = self;
    
    // KVC
    [self setValue:tabBar forKeyPath:@"tabBar"];
    
}
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 同时设置tabbar和navigationBar的文字
    childVc.navigationItem.title = title;
    
    // 设置子控制器的图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    childVc.view.backgroundColor = [UIColor whiteColor];
    if (childVc.class == [CFBTHomePageVC class]) {
        CFBTHomePageNavVC *nav = [[CFBTHomePageNavVC alloc] initWithRootViewController:childVc];
        // 导航控制器添加为TabBarController的子控制器
        [self addChildViewController:nav];
    } else{
        // 先给外面传进来的控制器 包装 一个导航控制器
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childVc];
        
        // 导航控制器添加为TabBarController的子控制器
        [self addChildViewController:nav];
    }
}

- (void)tabBarDidClickPlusButton:(BTTabBar *)tabBar {
    
    
}
@end
